﻿using System;
using System.Collections.Generic;
using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    /// <summary>
    /// 下拉菜单.
    /// 专用于显示文本的下拉框实现
    /// </summary>
    public class DropdownMenu : DropdownBox<string>
    {
        public static readonly float defaultFontSize = 12f;

        public float fontSize { get => _fontSize; set => SetFontSize(value); }

        private float _fontSize = defaultFontSize;

        public DropdownMenu()
        {
            SetItemDrawFunction(ItemDrawFunction, false);
        }
        public DropdownMenu(IList<string> data) : this()
        {
            SetData(data);
        }
        public DropdownMenu(float fontSize) : this()
        {
            _fontSize = fontSize;
        }
        public DropdownMenu(IList<string> data, float fontSize) : this(fontSize)
        {
            SetData(data);
        }
        public DropdownMenu(IList<string> data, Action<string> onSelectedCallback) : this(onSelectedCallback)
        {
            SetData(data);
        }
        public DropdownMenu(IList<string> data, Action<string> onSelectedCallback, float fontSize) : this(fontSize)
        {
            onItemSelected += onSelectedCallback;
            SetData(data);
        }
        public DropdownMenu(Action<string> onSelectedCallback) : this()
        {
            onItemSelected += onSelectedCallback;
        }

        public void SetFontSize(float newFontSize)
        {
            _fontSize = newFontSize;
            Refresh();
        }

        protected virtual VisualElement ItemDrawFunction(string data, int index)
        {
            ReadonlyText readonlyText = EditorElementUtility.elementFactory.CreateReadonlyText(false, false);
            readonlyText.text = data;
            readonlyText.style.fontSize = fontSize;
            readonlyText.style.textOverflow = TextOverflow.Ellipsis;
            readonlyText.style.unityTextOverflowPosition = TextOverflowPosition.Start;
            readonlyText.style.paddingLeft = readonlyText.style.paddingRight = 4f;
            return readonlyText;
        }
    }
}